package com.lzc.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.lzc.dao.ClientDao;
import com.lzc.pojo.Client;

public class ClientDaoImpl implements ClientDao{

	@Override
	public int ClientUploadDao(Client c) {
		Connection conn = null;
		PreparedStatement ps = null;
		int index = -1;
		try {
			//加载驱动
			Class.forName("com.mysql.cj.jdbc.Driver");
			//获取链接
			conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/2020?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false", "root", "19971003qaz");
			//创建SQl命令
			String sql = "insert into client values(default,?,?,?,?,?,?,?)";
			//创建SQL命令对象
			ps = conn.prepareStatement(sql);
			//给占位符赋值
			ps.setString(1, c.getUname());
			ps.setString(2, c.getPhone());
			ps.setString(3, c.getEmail());
			ps.setString(4, c.getSex());
			ps.setString(5, c.getBirth());
			ps.setString(6, c.getAddress());
			ps.setInt(7, c.getFlag());
			//执行SQL
			index = ps.executeUpdate();
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		

		return index;
	}

	@Override
	public List<Client> clientShowDao(String page) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Client> lc = null;
		try {
			//加载驱动
			Class.forName("com.mysql.cj.jdbc.Driver");
			//获取链接
			conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/2020?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false", "root", "19971003qaz");
			//创建SQl命令
			String sql = "select * from client limit ?,?";
			//创建SQL命令对象
			ps = conn.prepareStatement(sql);
			//给占位符赋值
			int start = Integer.parseInt(page) * 10;
			int end = start + 10;
			ps.setInt(1, start);
			ps.setInt(2, end);
			
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}

}
